package com.zhiyin.seal.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;

public class HeaderFooter extends PdfPageEventHelper {
    public HeaderFooter(String hTitle,String mark) {
        this.headerTitle = hTitle;
        this.mark = mark;
    }
    public HeaderFooter(String hTitle,String mark,Integer totalPages,Boolean showTotalPages) {
        this.headerTitle = hTitle;
        this.mark = mark;
        this.totalPages = totalPages;
        this.showTotalPages = showTotalPages;
    }
    private String headerTitle;
    private String mark;
    private Integer totalPages;
    private Boolean showTotalPages;


    /**
     * PDF添加页眉
     * @param title
     * @param writer
     * @param document
     */
    private void writeHeader(String title, PdfWriter writer, Document document) {
        try {
            BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            Font f = new Font(bfChinese,20, Font.BOLD);
            f.setFamily("微软雅黑");
            //添加页眉
            Paragraph pTitle = new Paragraph(title,f);
            pTitle.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(pTitle);
            //添加横线
            Paragraph pLine = new Paragraph();
            pLine.add(new Chunk(new LineSeparator()));
            document.add(pLine);
        } catch(Exception ex) {

        }
    }
    /**
     * PDF添加水印
     * @param writer
     * @param mark
     */
    private void addWaterMark(PdfWriter writer, String mark) {
        // 加入水印
        PdfContentByte waterMar = writer.getDirectContentUnder();
        // 开始设置水印
        waterMar.beginText();
        // 设置水印透明度
        PdfGState gs = new PdfGState();
        // 设置填充字体不透明度为0.2f
        gs.setFillOpacity(0.2f);
        try {
            BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            // 设置水印字体参数及大小                                  (字体参数，字体编码格式，是否将字体信息嵌入到pdf中（一般不需要嵌入），字体大小)
            waterMar.setFontAndSize(bfChinese, 30);
            // 设置透明度
            waterMar.setGState(gs);
            // 设置水印对齐方式 水印内容, x坐标yx坐标 旋转角度
            waterMar.showTextAligned(Element.ALIGN_LEFT, mark , -100, 550, 45);
            waterMar.showTextAligned(Element.ALIGN_LEFT, mark , 150, 600, 45);
            waterMar.showTextAligned(Element.ALIGN_LEFT, mark , 50, 300, 45);
            waterMar.showTextAligned(Element.ALIGN_LEFT, mark , 300, 350, 45);
            waterMar.showTextAligned(Element.ALIGN_LEFT, mark , 150, 50, 45);
            waterMar.showTextAligned(Element.ALIGN_LEFT, mark , 500, 150, 45);
            // 设置水印颜色
            waterMar.setColorFill(BaseColor.GRAY);
            //结束设置
            waterMar.endText();
            waterMar.stroke();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            waterMar = null;
            gs = null;
        }
    }

    @Override
    public void onStartPage(PdfWriter writer,Document document) {
        //添加水印
        addWaterMark(writer,this.mark);
        //添加页眉
        if(this.headerTitle !=null){
            writeHeader(this.headerTitle,writer,document);
        }
    }
    @Override
    public void onEndPage(PdfWriter pdfWriter, Document document) {
        try {
            if(totalPages != null && showTotalPages != null){
                if(showTotalPages){
                    //添加页码
                    ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_CENTER
                            ,new Phrase(String.format("%d/%d",pdfWriter.getPageNumber(),this.totalPages)),300,50,0);
                } else {
                    //添加页码
                    ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_CENTER
                            ,new Phrase(String.format("%d",pdfWriter.getPageNumber())),300,50,0);
                }
            }



        } catch(Exception ex) {

        }

    }
}
